<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ob:plan:bill:declare:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      action-width="240"
      :row-class-name="rowClassName"
    >
      <template #beforejobContent>
        <el-table-column
          label="票据号"
          align="center"
          key="billNumber"
          prop="billNumber"
          width="200"
        />
      </template>
      <!-- <template #applyCompanyId="{ row }">{{ row.applyCompany }}</template> -->
      <template #jobPersonnel="{ row }"> {{ row.jobPersonnelName }}</template>
      <template #monitorLocal="{ row }"> {{ row.monitorLocalName }}</template>
      <template #gasLocal="{ row }"> {{ row.gasLocalName }}</template>
      <template #jobType="{ row }"> {{ row.jobTypeName }}</template>
      <template #default>
        <el-table-column label="暂停记录" align="center" width="200">
          <template #default="{ row }">
            <el-button type="text" @click="handleDetail(row)">查看</el-button>
          </template>
        </el-table-column>
      </template>
      <template #actionButton="{ row }">
        <el-button
          type="text"
          @click="handleEdit(row)"
          v-hasPermi="['ob:plan:bill:declare:edit']"
          >修改</el-button
        >
        <el-button type="text" @click="handleXuiqian(row)">续签</el-button>
        <!-- <el-button
          type="text"
          :disabled="disabledButton(row)"
          @click="handleStop(row)"
        >
          {{ row.operateStatus == 1 ? "恢复" : "暂停" }}
        </el-button> -->
        <el-button type="text" @click="openMap(row)">查看区域</el-button>
        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['ob:plan:bill:declare:delete']"
          >删除</el-button
        >
      </template>
    </Table>

    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      width="60%"
      :title="dialogType == 'add' ? '申报作业计划' : '修改作业计划'"
      :showButton="true"
      :slots="[
        'custody',
        'coordinate',
        'jobPersonnel',
        'jobPersonCharge',
        'approvedBy',
        'examinationPeople',
        // 'monitorLocal',
        // 'gasLocal',
        'applyPeople',
        'applyCompany',
        'address',
        'longitude',
        'latitude',
		'maxPersonNum'
      ]"
    >
      <template #applyCompany>
        <el-form-item
          label="作业申请部门"
          prop="applyCompanyId"
          label-position="right"
        >
          <TreeSelect
            v-model="form.applyCompanyId"
            :options="deptTree"
            @change="changeDept"
            style="width: 100%"
          >
          </TreeSelect>
        </el-form-item>
      </template>
      <template #monitorLocal>
        <el-form-item label="涉及监控点位" label-position="top">
          <el-button @click="onInnerOpen('monitorLocal')" type="primary"
            >选 择</el-button
          >
        </el-form-item>
        <el-form-item label="" label-position="top">
          <el-table ref="dragTable" :data="form.monitorLocal1">
            <el-table-column
              label="监控点位名称"
              prop="monitorName"
              :show-overflow-tooltip="true"
            />
          </el-table>
        </el-form-item>
      </template>
      <template #gasLocal>
        <el-form-item label="涉及气体位号" label-position="top">
          <el-button @click="onInnerOpen('gasLocal')" type="primary"
            >选 择</el-button
          >
        </el-form-item>
        <el-form-item label="" label-position="top">
          <el-table ref="dragTable" :data="form.gasLocal1">
            <el-table-column
              label="气体位号"
              prop="bitNumber"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="安装位置"
              prop="installAddress"
              :show-overflow-tooltip="true"
            />
          </el-table>
        </el-form-item>
      </template>
      <template #jobPersonnel>
        <el-form-item label="作业人员" label-position="right" required>
          <el-button @click="onInnerOpen('jobPersonnelout')" type="primary"
            >外部人员</el-button
          >
          <el-button @click="onInnerOpen('jobPersonnelin')" type="primary"
            >内部人员</el-button
          >
        </el-form-item>
        <el-form-item label="" label-position="right">
          <el-table ref="dragTable" :data="form.jobPersonnel1">
            <el-table-column
              label="姓名"
              align="center"
              key="name"
              prop="name"
            />

            <el-table-column label="人员类型" align="center">
              <template #default="{ row }">
                {{ row.type == "in" ? "内部人员" : "外部人员" }}
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template #default="scope">
                <el-button type="danger" @click="delZyry(scope)"
                  >删除</el-button
                >
              </template></el-table-column
            >
          </el-table>
        </el-form-item>
      </template>

      <template #jobPersonCharge="scope">
        <el-form-item
          label="作业负责人"
          prop="jobPersonChargeId"
          label-position="right"
        >
          <el-select
            v-model="form.jobPersonChargeId"
            @change="
              changeZy('jobPersonCharge', $event, selectZyry, scope.form)
            "
            style="width: 100%"
          >
            <el-option
              v-for="ele in selectZyry"
              :key="ele.id"
              :value="ele.id + ''"
              :label="ele.name"
            ></el-option>
          </el-select>
        </el-form-item>
      </template>
      <template v-for="item in slots" #[item.key]="scope">
        <el-form-item :label="item.name" label-position="right">
          <el-input
            v-model="form[item.key]"
            readonly
            @click="onInnerOpen(item.key, scope.form)"
          >
          </el-input>
        </el-form-item>
      </template>

      <TableSelect
        ref="userTableSelect"
        :queryApi="listUser"
        :options="[]"
        :rowKey="'userId'"
        rowLabel="nickName"
        @onConfirm="onUserConfirm"
        rowLabelText="用户昵称"
      >
        <template #formSlot="{ handleQuery, filterForm }">
          <el-col :span="8">
            <el-form-item label="部门名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.deptName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.userName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #tableSlot>
          <el-table-column
            label="部门"
            align="center"
            key="deptName"
            prop="dept.deptName"
          />
          <el-table-column
            label="用户名称"
            align="center"
            key="userName"
            prop="userName"
          />
          <el-table-column
            label="用户昵称"
            align="center"
            key="nickName"
            prop="nickName"
          />
        </template>
      </TableSelect>
      <TableSelect
        ref="personTableSelect"
        :queryApi="SGRY.queryFlag"
        :options="[]"
        :rowKey="'id'"
        rowLabel="name"
        @onConfirm="onUserConfirm"
        rowLabelText="姓名"
      >
        <template #formSlot="{ handleQuery, filterForm }">
          <el-col :span="8">
            <el-form-item label="单位名称">
              <el-input
                @change="handleQuery"
                v-model="filterForm.company"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #tableSlot>
          <el-table-column label="姓名" align="center" key="name" prop="name" />
          <el-table-column
            label="身份证号"
            align="center"
            key="cardNumber"
            prop="cardNumber"
          />
          <el-table-column
            label="单位名称"
            align="center"
            key="company"
            prop="company"
          />
          <el-table-column
            label="人员类型"
            align="center"
            key="personnelType"
            prop="personnelType"
          />
        </template>
      </TableSelect>
      <TableSelect
        ref="spjkTableSelect"
        :queryApi="SPJK.query"
        :options="[]"
        :rowKey="'id'"
        rowLabel="monitorName"
        @onConfirm="onUserConfirm"
        rowLabelText="监控点位名称"
      >
        <template #formSlot="{ handleQuery, filterForm }">
          <el-col :span="8">
            <el-form-item label="监控单位编号">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.monitorCode"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #tableSlot>
          <el-table-column
            label="监控点位名称"
            align="center"
            key="monitorName"
            prop="monitorName"
          />
          <el-table-column
            label="监控点位编号"
            align="center"
            key="monitorCode"
            prop="monitorCode"
          />
          <el-table-column
            label="位置描述"
            align="center"
            key="addressInfo"
            prop="addressInfo"
          />
        </template>
      </TableSelect>
      <TableSelect
        ref="qtjcTableSelect"
        :queryApi="QTJC.query"
        :options="[]"
        :rowKey="'id'"
        rowLabel="bitNumber"
        @onConfirm="onUserConfirm"
        rowLabelText="位号"
      >
        <template #formSlot="{ handleQuery, filterForm }">
          <el-col :span="8">
            <el-form-item label="部门名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.deptName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="安装位置">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.installAddress"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #tableSlot>
          <el-table-column
            label="部门"
            align="center"
            key="deptName"
            prop="deptName"
          />
          <el-table-column
            label="位号"
            align="center"
            key="bitNumber"
            prop="bitNumber"
          />
          <el-table-column
            label="安装位置"
            align="center"
            key="installAddress"
            prop="installAddress"
          />
        </template>
      </TableSelect>
      <template #address>
        <el-form-item label="区域绘制" prop="address">
          <el-button type="primary" @click="openMap(null)">位置维护</el-button>
        </el-form-item>
      </template>

      <template #longitude>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" />
        </el-form-item>
      </template>

      <template #latitude>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" />
        </el-form-item>
      </template>
	  
	  <template #maxPersonNum v-if="form.latitude">
	    <el-form-item label="报警人数" prop="maxPersonNum">
	      <el-input v-model="form.maxPersonNum" />
	    </el-form-item>
	  </template>
	  
    </DialogVform>
    <MapDialog
      ref="map"
      mapId="sb"
      @change="mapChange"
      :useButton="useButton"
    ></MapDialog>
    <el-dialog v-model="outerVisible" width="90%" title="暂停恢复记录">
      <Inner ref="inner" :parentNode="parentNode"></Inner>
    </el-dialog>
    <el-dialog v-model="stopDialog" width="45%" title="暂停/恢复">
      <el-form ref="stopRef" :model="stopForm" :rules="stopRules">
        <!-- <h3 class="title">后台管理系统</h3> -->、
        <el-form-item prop="operateTime" label="时间">
          <el-date-picker
            type="datetime"
            v-model="stopForm.operateTime"
            clearable
            style="width: 100%"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item prop="remark" label="描述">
          <el-input
            v-model="stopForm.remark"
            type="textarea"
            size="large"
            placeholder="描述"
          >
          </el-input>
        </el-form-item>

        <el-form-item style="width: 100%">
          <div style="width: 100%; display: flex; justify-content: flex-end">
            <el-button :loading="loading" type="primary" @click="submitStopForm"
              >确认
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog v-model="xuqianDialog" width="45%" title="续签">
      <el-form ref="xuqianRef" :model="xqForm" :rules="xuqianRules">
        <el-form-item prop="jobPlanStartDate" label="续签开始日期">
          <el-date-picker
            type="date"
            v-model="xqForm.jobPlanStartDate"
            clearable
            style="width: 100%"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item prop="jobPlanStartTime" label="续签开始时间">
          <el-select v-model="xqForm.jobPlanStartTime" style="width: 100%">
            <el-option
              v-for="ele in times"
              :key="ele.value"
              :value="ele.value"
              :label="ele.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="jobPlanEndDate" label="续签结束日期">
          <el-date-picker
            type="date"
            v-model="xqForm.jobPlanEndDate"
            clearable
            style="width: 100%"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item prop="jobPlanEndTime" label="续签结束时间">
          <el-select v-model="xqForm.jobPlanEndTime" style="width: 100%">
            <el-option
              v-for="ele in times"
              :key="ele.value"
              :value="ele.value"
              :label="ele.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="width: 100%">
          <div style="width: 100%; display: flex; justify-content: flex-end">
            <el-button :loading="loading" type="primary" @click="submitxqForm"
              >确认
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS, stopAndRestart } from "@/api/zygl/zysb.js";
import formJson from "./components/options.json";
import { listUser, deptTreeSelect } from "@/api/system/user";
import TableSelect from "@/components/tableSelect/index";
import { apis as SPJK } from "@/api/hdwarning/spjkxx/index.js";
import { apis as QTJC } from "@/api/hdwarning/krydqt.js";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import { apis as ZYDW } from "@/api/zygl/tszyhjpz.js";
import { apis as SGRY } from "@/api/aqsc/xgfgl/lssgrytz.js";
import MapDialog from "@/components/mapDialog/index";
import { transform } from "ol/proj";
import Inner from "./components/inner.vue";
const { proxy } = getCurrentInstance();
const ops = useFormJson(formJson, {
  sort: ["jobContent", "local"],
  sortable: [],
});
const options = reactive({
  table: ops.filter((i) => i.prop != "jobPersonChargeId"),
  formOps: ops.filter((i) =>
    [
      "riskLevel",
      "applyCompany",
      "degree",
      "jobPlanStartDate",
      "izManual",
      "billNumber",
      "local",
      "workShop",
      "declareType",
    ].includes(i.prop)
  ),
});
const slots = ref([
  // {
  //   name: "作业监护人",
  //   key: "custody",
  // },
  // {
  //   name: "施工协调员",
  //   key: "coordinate",
  // },
  // // {
  // //   name: "作业负责人",
  // //   key: "jobPersonCharge",
  // // },
  // {
  //   name: "现场批准人",
  //   key: "approvedBy",
  // },
  // {
  //   name: "措施审查人",
  //   key: "examinationPeople",
  // },
  {
    name: "申请人",
    key: "applyPeople",
  },
]);
const showSearch = ref(true);
const dialogVform = ref();
const table = ref();
const userTableSelect = ref();
const userType = ref("");
const spjkTableSelect = ref();
const qtjcTableSelect = ref();
const personTableSelect = ref();
// 选择作业人员逻辑
const selectZyry = computed(() => {
  if (form.value.jobPersonnel1 && form.value.jobPersonnel1.length) {
    // return form.value.jobPersonnel1.filter((item) => item.type == "in");
    return form.value.jobPersonnel1;
  } else {
    return [];
  }
});
const delZyry = (row) => {
  form.value.jobPersonnel1.splice(row.$index, 1);
};
let formData = {};
// 选择弹窗列表
const onInnerOpen = (type, data) => {
  formData = data;
  userType.value = type;
  if (type == "jobPersonnelout") {
    personTableSelect.value.open([]);
    return;
  }
  if (type == "jobPersonnelin") {
    userTableSelect.value.open([]);
    return;
  }
  if (type == "gasLocal") {
    qtjcTableSelect.value.open(form.value.gasLocal1);
    return;
  }
  if (type == "monitorLocal") {
    spjkTableSelect.value.open(form.value.monitorLocal1);
    return;
  }

  let values =
    (form.value[type + "Id"] && form.value[type + "Id"].split(",")) || [];
  let names = (form.value[type] && form.value[type].split(",")) || [];
  userTableSelect.value.open(
    values.map((item, index) => ({
      value: values[index],
      label: names[index],
    }))
  );
};
const onUserConfirm = (data) => {
  if (userType.value == "jobPersonnelin") {
    if (!form.value.jobPersonnel1) {
      form.value.jobPersonnel1 = [];
    }
    const ids = form.value.jobPersonnel1
      .filter((item) => item.type == "in")
      .map((user) => user.id);
    const newData = data
      .filter((user) => !ids.includes(user.userId))
      .map((ele) => ({
        name: ele.nickName,
        id: ele.userId,
        type: "in",
        zsh: ele.cardNumber,
      }));
    form.value.jobPersonnel1 = [...form.value.jobPersonnel1, ...newData];
    return;
  }
  if (userType.value == "jobPersonnelout") {
    if (!form.value.jobPersonnel1) {
      form.value.jobPersonnel1 = [];
    }
    const ids = form.value.jobPersonnel1
      .filter((item) => item.type == "out")
      .map((user) => user.id);
    const newData = data
      .filter((user) => !ids.includes(user.id))
      .map((ele) => ({
        name: ele.name,
        id: ele.id,
        type: "out",
        zsh: ele.certificateNumber,
      }));
    form.value.jobPersonnel1 = [...form.value.jobPersonnel1, ...newData];
    // form.value.jobPersonnel1 = data;
    return;
  }
  if (userType.value == "gasLocal") {
    form.value.gasLocal1 = data;
    return;
  }
  if (userType.value == "monitorLocal") {
    form.value.monitorLocal1 = data;
    return;
  }
  const values = data.map((item) => item.value);
  const names = data.map((item) => item.label);
  form.value[userType.value] = names.join(",");
  formData[userType.value] = names.join(",");
  formData[userType.value + "Id"] = values.join(",");
  form.value[userType.value + "Id"] = values.join(",");
};
const plugSubmit = (data) => {
  const { monitorLocal1 } = form.value;
  try {
    form.value.jobPersonnel = JSON.stringify(form.value.jobPersonnel1 || []);
  } catch (error) {
    form.value.jobPersonnel = "[]";
  }
  try {
    form.value.monitorLocal = JSON.stringify(form.value.monitorLocal1 || []);
  } catch (error) {
    form.value.monitorLocal = "[]";
  }
  try {
    form.value.gasLocal = JSON.stringify(form.value.gasLocal1 || []);
  } catch (error) {
    form.value.gasLocal = "[]";
  }
  form.value.jobType = data.jobType.join(",");
  
  if (points && points.value!=null && points.value!='') {
        let epsg4326_ = [];
		let epsg3857_ = points.value;
		for (let i=0; i<epsg3857_[0].length; i++) {
			// console.log("epsg3857_[0][i] : " + epsg3857_[0][i]);
			// console.log(transform(epsg3857_[0][i], 'EPSG:3857', 'EPSG:4326'));
			epsg4326_.push(transform(epsg3857_[0][i], 'EPSG:3857', 'EPSG:4326'));
		}
		form.value.drawArea2 = JSON.stringify(epsg4326_);
	    form.value.drawArea = JSON.stringify(points.value);
  }
  // console.log("form.value.drawArea2 : " + form.value.drawArea2);

  // if (
  //   form.value.jobType.includes("一级") ||
  //   form.value.jobType.includes("特级")
  // ) {
  //   if (!monitorLocal1 || monitorLocal1.length == 0) {
  //     proxy.$modal.msgError("作业为特级或一级时监控点位必选");
  //     throw new Error("作业为特级或一级时监控点位必选");
  //   }
  // }
  
  return {
    ...data,
    jobType: data.jobType.join(","),
    drawArea: JSON.stringify(points.value),
  };


	  // epsg4326Coord.value = data.epsg4326Coord
	  // form.value.longitude = epsg4326Coord.value[0]
	  // form.value.latitude = epsg4326Coord.value[1]
};
// 补充修改.新增事件
const showButton = ref(true);
const plugAdd = () => {
  showButton.value = true;
  form.value.jobPersonnel1 = [];
  form.value.monitorLocal1 = [];
  form.value.gasLocal1 = [];
  points.value = [];
};
const plugEdit = (row) => {
  form.value.longitude = row.longitude;
  form.value.latitude = row.latitude;
  form.value.maxPersonNum = row.maxPersonNum;
  showButton.value = false;
  form.value.jobPersonnel1 = row.jobPersonnel || [];
  form.value.monitorLocal1 = row.monitorLocal || [];
  form.value.gasLocal1 = row.gasLocal || [];
  form.value.jobType = row.jobType;
  form.value.custody = row.custody;
  form.value.coordinate = row.coordinate;
  form.value.applyCompanyId = row.applyCompanyId;
  form.value.jobPersonnel = row.jobPersonnel;
  form.value.jobPersonCharge = row.jobPersonCharge;
  form.value.approvedBy = row.approvedBy;
  form.value.examinationPeople = row.examinationPeople;
  form.value.custodyId = row.custodyId;
  form.value.coordinateId = row.coordinateId;
  form.value.jobPersonnelId = row.jobPersonnelId;
  form.value.jobPersonChargeId = row.jobPersonChargeId;
  form.value.approvedById = row.approvedById;
  form.value.applyPeople = row.applyPeople;
  form.value.applyPeopleId = row.applyPeopleId;
  form.value.examinationPeopleId = row.examinationPeopleId;
  form.value.drawArea = row.drawArea;
};

const epsg4326Coord = ref([])

const formatList = (list) => {
  return list.map((item) => {
    let jobPersonnel = [];
    let monitorLocal = [];
    let gasLocal = [];

    try {
      jobPersonnel = JSON.parse(item.jobPersonnel) || [];
    } catch (error) {
      jobPersonnel = [];
    }
    try {
      monitorLocal = JSON.parse(item.monitorLocal) || [];
    } catch (error) {
      monitorLocal = [];
    }
    try {
      gasLocal = JSON.parse(item.gasLocal) || [];
    } catch (error) {
      gasLocal = [];
    }
    return {
      ...item,
      jobPersonnel,
      monitorLocal,
      gasLocal,
      jobType: (item.jobType && item.jobType.split(",")) || [],
      jobPersonnelName: jobPersonnel.map((item) => item.name).join(","),
      monitorLocalName: monitorLocal.map((item) => item.monitorName).join(","),
      gasLocalName: gasLocal.map((item) => item.bitNumber).join(","),
      jobTypeName: item.jobType,
      applyCompanyId:(item.applyCompanyId && Number(item.applyCompanyId)) || null
    };
  });
};

const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugEdit,
  plugAdd,
  formatList,
  plugSubmit,
});
const zydwList = ref({});
const deptTree = ref([]);
onMounted(() => {
  ZYDW.query({ pageNum: 1, pageSize: 999999999 }).then((res) => {
    res.rows.forEach((item) => {
      zydwList.value[item.id] = item.linkName;
    });
  });
  deptTreeSelect().then((response) => {
    deptTree.value = response.data;
  });
});
const changeDept = (val, label) => {
  form.value.applyCompany = label;
  form.value.applyCompanyId = val;
};
const changeZy = (prop, val, list, data) => {
  if (!val) {
    form.value[prop] = "";
    data.jobPersonChargeId = "";
    data[prop] = "";
  } else {
    form.value[prop] = list.find((item) => item.id == val)?.name || "";
    data.jobPersonChargeId = val;
    data[prop] = form.value[prop];
  }
};

const useButton = ref(true);
const map = ref();
const points = ref([]);

const openMap = (row) => {
  if (row && row.drawArea) {
    useButton.value = false;
    map.value.open(JSON.parse(row.drawArea));
    return;
  }
  if (row) {
    useButton.value = false;
    map.value.open([]);
  } else {
    useButton.value = true;
    map.value.open(points.value);

    // console.log('drawArea: ', JSON.parse('121.462971','31.214170'))
    // useButton.value = false;
    // map.value.open('31.214170','121.462971');
  }
};
const mapChange = (data) => {
  points.value = data.points;
  if(data && data.epsg4326Coord){
    epsg4326Coord.value = data.epsg4326Coord;
	let l1 = epsg4326Coord.value[0];
	let l2 = epsg4326Coord.value[1];
    form.value.longitude = l1!=null && l1>0 ? l1.toFixed(6) : "";
    form.value.latitude = l2!=null && l2>0 ? l2.toFixed(6) : "";
  }
};
const rowClassName = ({ row, rowIndex }) => {
  if (row.cancel === true) {
    return "red";
  } else {
    return "";
  }
};
const disabledButton = (row) => {
  const nowTime = Date.now();
  const endTime = new Date(row.jobPlanEndDate).getTime();
  if (nowTime > endTime) {
    return true;
  } else {
    return false;
  }
};
// 暂停、恢复
const inner = ref();
const stopDialog = ref(false);
const parentNode = ref({});
const outerVisible = ref(false);
const handleDetail = (row) => {
  outerVisible.value = true;
  nextTick().then(() => {
    parentNode.value = row;
  });
};
const stopForm = ref({
  remark: "",
  operateTime: "",
  pid: "",
});
const stopRules = {
  remark: [{ required: true, trigger: "blur", message: "请输入暂停描述" }],
  operateTime: [{ required: true, trigger: "blur", message: "请选择时间" }],
};
const handleStop = (row) => {
  stopDialog.value = true;
  stopForm.value.pid = row.id;
  stopForm.value.operateStatus = row.operateStatus;
  stopForm.value.currentStatus = row.operateStatus == 1 ? 2 : 1;
};
const stopRef = ref();
const submitStopForm = async () => {
  const res = await stopRef.value.validate();
  if (res) {
    stopAndRestart
      .add({
        ...stopForm.value,
      })
      .then((res) => {
        proxy.$modal.msgSuccess("操作成功");
        stopDialog.value = false;
        getList(true);
      });
  }
};
// 续签逻辑
const times = ref([
  { value: "00:00", label: "00:00" },
  { value: "00:30", label: "00:30" },
  { value: "01:00", label: "01:00" },
  { value: "01:30", label: "01:30" },
  { value: "02:00", label: "02:00" },
  { value: "02:30", label: "02:30" },
  { value: "03:00", label: "03:00" },
  { value: "03:30", label: "03:30" },
  { value: "04:00", label: "04:00" },
  { value: "04:30", label: "04:30" },
  { value: "05:00", label: "05:00" },
  { value: "05:30", label: "05:30" },
  { value: "06:00", label: "06:00" },
  { value: "06:30", label: "06:30" },
  { value: "07:00", label: "07:00" },
  { value: "07:30", label: "07:30" },
  { value: "08:00", label: "08:00" },
  { value: "08:30", label: "08:30" },
  { value: "09:00", label: "09:00" },
  { value: "09:30", label: "09:30" },
  { value: "10:00", label: "10:00" },
  { value: "10:30", label: "10:30" },
  { value: "11:00", label: "11:00" },
  { value: "11:30", label: "11:30" },
  { value: "12:00", label: "12:00" },
  { value: "12:30", label: "12:30" },
  { value: "13:00", label: "13:00" },
  { value: "13:30", label: "13:30" },
  { value: "14:00", label: "14:00" },
  { value: "14:30", label: "14:30" },
  { value: "15:00", label: "15:00" },
  { value: "15:30", label: "15:30" },
  { value: "16:00", label: "16:00" },
  { value: "16:30", label: "16:30" },
  { value: "17:00", label: "17:00" },
  { value: "17:30", label: "17:30" },
  { value: "18:00", label: "18:00" },
  { value: "18:30", label: "18:30" },
  { value: "19:00", label: "19:00" },
  { value: "19:30", label: "19:30" },
  { value: "20:00", label: "20:00" },
  { value: "20:30", label: "20:30" },
  { value: "21:00", label: "21:00" },
  { value: "21:30", label: "21:30" },
  { value: "22:00", label: "22:00" },
  { value: "22:30", label: "22:30" },
  { value: "23:00", label: "23:00" },
  { value: "23:30", label: "23:30" },
]);
const xuqianDialog = ref(false);
const handleXuiqian = (row) => {
  xuqianDialog.value = true;
  xqForm.value.id = row.id;
};
const xqForm = ref({
  jobPlanEndDate: "",
  jobPlanEndTime: "",
  jobPlanStartDate: "",
  jobPlanStartTime: "",
  id: "",
});
const xuqianRules = {
  jobPlanEndDate: [{ required: true, trigger: "blur", message: "请选择日期" }],
  jobPlanEndTime: [{ required: true, trigger: "blur", message: "请选择时间" }],
  jobPlanStartDate: [
    { required: true, trigger: "blur", message: "请选择日期" },
  ],
  jobPlanStartTime: [
    { required: true, trigger: "blur", message: "请选择时间" },
  ],
};
const xuqianRef = ref();
const submitxqForm = async () => {
  const res = await xuqianRef.value.validate();
  if (res) {
    APIS.renewal({
      ...xqForm.value,
    }).then((res) => {
      proxy.$modal.msgSuccess("操作成功");
      xuqianDialog.value = false;
      getList(true);
    });
  }
};
</script>

<style lang="scss" scoped>
::v-deep tr.red {
  background-color: red !important;
  color: #000 !important;
}
</style>
